pub mod job;
mod trader_virtual;

use away_db::{model::appstate::AppState, trader_repository::stock_order_rep::AddOrder};
use axum::{extract::State, response::IntoResponse, routing::post, Json, Router};

pub fn add_trader_route(appstate: AppState) -> Router {
    Router::new()
        .route("/trader/buy_or_sell", post(buy_or_sell))
        .route("/trader/cancel", post(cancel))
        .with_state(appstate)
}

async fn buy_or_sell(
    State(appstate): State<AppState>,
    Json(dto): Json<AddOrder>,
) -> impl IntoResponse {
    let res = trader_virtual::buy_or_sell(&appstate.db, dto).await;
    Json(res)
}

async fn cancel(
    State(appstate): State<AppState>,
    Json(dto): Json<OrderCancel>,
) -> impl IntoResponse {
    let res = trader_virtual::cancel(&appstate.db, dto.uid, dto.oid).await;
    Json(res)
}

#[derive(serde::Serialize, serde::Deserialize)]
struct OrderCancel {
    uid: i32,
    oid: i32,
}
